package com.seeyon.apps.enxicun.event;

import com.seeyon.apps.enxicun.manager.EnxicunManager;
import com.seeyon.apps.enxicun.utils.HttpRequest;
import com.seeyon.ctp.common.AppContext;
import com.seeyon.ctp.common.exceptions.BusinessException;
import com.seeyon.ctp.organization.bo.V3xOrgDepartment;
import com.seeyon.ctp.organization.bo.V3xOrgMember;
import com.seeyon.ctp.organization.event.*;
import com.seeyon.ctp.organization.manager.OrgManager;
import com.seeyon.ctp.util.annotation.ListenEvent;
import com.seeyon.ctp.util.json.JSONUtil;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;


/**
 * @Classname OrganizationEventListener
 * @Description 人员监听
 * @Date 2022/3/22 15:12
 * @Created by Administrator
 */
public class MemberEventListener {
    private static Log log = LogFactory.getLog(MemberEventListener.class);// 打印日志
    private OrgManager orgManager = (OrgManager) AppContext.getBean("orgManager");
    private static String ip = AppContext.getSystemProperty("enxicun.ip");
    private static String access_token = AppContext.getSystemProperty("enxicun.access_token");
    private static V3xOrgMember member = null;
    Map<String, Object> hashMap = null;


    @ListenEvent(
            event = UpdateMemberEvent.class,
            async = true
    )
    public void updateMember(UpdateMemberEvent event) {
        log.info("修改实时同步");
        member = event.getMember();
        log.info("member转json:" + JSONUtil.toJSONString(member));

        hashMap = new HashMap<String, Object>();
        hashMap.put("pin", member.getCode());
        //通过部门编号取得部门code
        V3xOrgDepartment department = null;
        try {
            department = orgManager.getDepartmentById(member.getOrgDepartmentId());
        } catch (BusinessException e) {
            e.printStackTrace();
        }
        hashMap.put("deptCode", department.getCode());
        hashMap.put("birthday", member.getBirthday());
        hashMap.put("certNumber", member.getIdNum());
        hashMap.put("email", member.getEmailAddress());
        hashMap.put("gender", gender(member.getGender().toString()));
        hashMap.put("hireDate", member.getHiredate());
        hashMap.put("mobilePhone", member.getTelNumber());
        hashMap.put("name", member.getName());

        log.info(JSONUtil.toJSONString(hashMap));

        try {
            HttpRequest.getJsonData(ip + "/api/person/add/?access_token=" + access_token, JSONUtil.toJSONString(hashMap));
        } catch (Exception e) {
            log.error(e);
        }

    }

    //删除监听
    @ListenEvent(
            event = DeleteMemberEvent.class,
            async = true
    )
    public void deleteMember(DeleteMemberEvent event) {
        log.info("删除实时同步");
        member = event.getMember();
        log.info("member转json:" + JSONUtil.toJSONString(member));

        String pin = member.getCode();

        try {
            HttpRequest.getJsonData(ip + "/api/person/delete/" + pin + "?access_token=" + access_token, "");
        } catch (Exception e) {
            log.error(e);
        }

    }


    //新增监听
    @ListenEvent(
            event = AddMemberEvent.class,
            async = true
    )
    public void onAddMember(AddMemberEvent event) {
        log.info("新增实时同步");
        member = event.getMember();
        log.info("member转json:" + JSONUtil.toJSONString(member));

        hashMap = new HashMap<String, Object>();
        hashMap.put("pin", member.getCode());
        //通过部门编号取得部门code
        V3xOrgDepartment department = null;
        try {
            department = orgManager.getDepartmentById(member.getOrgDepartmentId());
        } catch (BusinessException e) {
            e.printStackTrace();
        }
        hashMap.put("deptCode", department.getCode());
        hashMap.put("birthday", member.getBirthday());
        hashMap.put("certNumber", member.getIdNum());
        hashMap.put("email", member.getEmailAddress());
        hashMap.put("gender", gender(member.getGender().toString()));
        hashMap.put("hireDate", member.getHiredate());
        hashMap.put("mobilePhone", member.getTelNumber());
        hashMap.put("name", member.getName());

        log.info(JSONUtil.toJSONString(hashMap));

        try {
            HttpRequest.getJsonData(ip + "/api/person/add/?access_token=" + access_token, JSONUtil.toJSONString(hashMap));
        } catch (Exception e) {
            log.error(e);
        }

    }

    public String gender(String gender) {
        if ("1".equals(gender)) {
            return "M";
        } else if ("2".equals(gender)) {
            return "F";
        }
        return "";
    }


}
